package lex.mtd.ui.download;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.*;
import javax.swing.event.DocumentEvent;

import org.lex.awt.SystemClipboard;
import org.lex.io.filesystem.FileNameUtilities;
import org.lex.swing.DocumentAdapter;
import org.lex.swing.Swings;
import org.lex.utils.text.Splitter;

import lex.mtd.Download;
import lex.mtd.core.DownloadObject;
import lex.mtd.core.utils.ProtocolUtils;
import lex.mtd.utils.prefs.Keys;

public class NewTaskPane extends javax.swing.JPanel {
	private JButton cancelButton;
	private JPanel generalPane;
	private JPanel advancedPane;
	private JLabel urlLabel;
	private JTextArea textArea;
	private JScrollPane scrollPane;
	private JTextField mainUrlTextField;
	private JLabel mainUrlLabel;
	private JLabel threadLabel;
	private JSpinner threadSpinner;
	private JTextField renameTextField;
	private JLabel renameLabel;
	private JButton directoryButton;
	private JTextField directoryTextField;
	private JLabel directoryLabel;
	private JTextField urlTextField;
	private JTabbedPane tabbedPane;
	private JButton okButton;
	private SpinnerNumberModel spinnerModel;

	public static void main(String[] args) {
		Swings.show(new NewTaskPane());
	}

	public NewTaskPane() {
		super();
		initialize();
	}

	DownloadObject createDownloadTask() {
		DownloadObject task = new DownloadObject();
		task.getURLSet().addURL(this.getUrlTextField().getText());
		task.setDirectory(new File(this.getDirectoryTextField().getText()));
		task.setFileName(this.getRenameTextField().getText());
		task.setThreadLimit((Integer) this.getThreadSpinner().getValue());
		String addresses = this.getTextArea().getText().trim();
		String[] urls = Splitter.BlankSpaceSplitter.split(addresses);
		task.getURLSet().addURL(urls);
		return task;
	}

	JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
		}
		return cancelButton;
	}

	JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("OK");
		}
		return okButton;
	}

	private JButton getDirectoryButton()
	{
		if( directoryButton == null )
		{
			directoryButton = new JButton();
			directoryButton.setText( "..." );
			directoryButton.addActionListener( new ActionListener(){
				@Override
				public void actionPerformed( ActionEvent e )
				{
					File init = new File( getDirectoryTextField().getText() );
					JFileChooser fc = new JFileChooser( init );
					fc.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
					fc.setMultiSelectionEnabled( false );
					int opt = fc.showOpenDialog( NewTaskPane.this );
					if( JFileChooser.APPROVE_OPTION == opt )
					{
						File dir = fc.getSelectedFile();
						if( false == dir.exists() || false == dir.isDirectory() )
							dir.mkdirs();
						directoryTextField.setText( dir.getAbsolutePath() );
					}
				}
			} );
		}
		return directoryButton;
	}

	private void initialize() {
		GroupLayout thisLayout = new GroupLayout(this);
		this.setLayout(thisLayout);
		thisLayout.setVerticalGroup(thisLayout
				.createSequentialGroup()
				.addContainerGap()
				.addComponent(getTabbedPane(), 0, 274, Short.MAX_VALUE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addGroup(
						thisLayout
								.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(getCancelButton(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(getOkButton(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE,
										GroupLayout.PREFERRED_SIZE)).addContainerGap());
		thisLayout.setHorizontalGroup(thisLayout
				.createSequentialGroup()
				.addContainerGap()
				.addGroup(
						thisLayout
								.createParallelGroup()
								.addComponent(getTabbedPane(), GroupLayout.Alignment.LEADING, 0, 342, Short.MAX_VALUE)
								.addGroup(
										GroupLayout.Alignment.LEADING,
										thisLayout
												.createSequentialGroup()
												.addGap(0, 240, Short.MAX_VALUE)
												.addComponent(getOkButton(), GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED, 1, GroupLayout.PREFERRED_SIZE)
												.addComponent(getCancelButton(), GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE))).addContainerGap());
		thisLayout.linkSize(SwingConstants.HORIZONTAL, new Component[] { getOkButton(), getCancelButton() });
		this.setPreferredSize(new Dimension(235, 300));
	}

	private JTabbedPane getTabbedPane() {
		if (tabbedPane == null) {
			tabbedPane = new JTabbedPane();
			tabbedPane.addTab("General", null, getGeneralPane(), null);
			tabbedPane.addTab("Advanced", null, getAdvancedPane(), null);
		}
		return tabbedPane;
	}

	private JPanel getGeneralPane() {
		if (generalPane == null) {
			generalPane = new JPanel();
			GroupLayout generalPaneLayout = new GroupLayout(generalPane);
			generalPane.setLayout(generalPaneLayout);
			generalPaneLayout.setHorizontalGroup(generalPaneLayout
					.createSequentialGroup()
					.addContainerGap()
					.addGroup(
							generalPaneLayout
									.createParallelGroup()
									.addGroup(
											GroupLayout.Alignment.LEADING,
											generalPaneLayout.createSequentialGroup()
													.addComponent(getUrlLabel(), GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
													.addGap(14))
									.addGroup(
											GroupLayout.Alignment.LEADING,
											generalPaneLayout
													.createSequentialGroup()
													.addComponent(getRenameLabel(), GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE,
															GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED))
									.addComponent(getDirectoryLabel(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE,
											GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
									.addGroup(
											GroupLayout.Alignment.LEADING,
											generalPaneLayout
													.createSequentialGroup()
													.addComponent(getThreadLabel(), GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE,
															GroupLayout.PREFERRED_SIZE).addGap(10)))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(
							generalPaneLayout
									.createParallelGroup()
									.addGroup(
											GroupLayout.Alignment.LEADING,
											generalPaneLayout
													.createSequentialGroup()
													.addGroup(
															generalPaneLayout
																	.createParallelGroup()
																	.addComponent(getDirectoryTextField(), GroupLayout.Alignment.LEADING, 0, 58,
																			Short.MAX_VALUE)
																	.addGroup(
																			GroupLayout.Alignment.LEADING,
																			generalPaneLayout
																					.createSequentialGroup()
																					.addComponent(getThreadSpinner(), GroupLayout.PREFERRED_SIZE,
																							GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
																					.addGap(17)))
													.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
													.addComponent(getDirectoryButton(), GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE,
															GroupLayout.PREFERRED_SIZE))
									.addComponent(getRenameTextField(), GroupLayout.Alignment.LEADING, 0, 90, Short.MAX_VALUE)
									.addComponent(getUrlTextField(), GroupLayout.Alignment.LEADING, 0, 90, Short.MAX_VALUE)).addContainerGap());
			generalPaneLayout.setVerticalGroup(generalPaneLayout
					.createSequentialGroup()
					.addContainerGap()
					.addGroup(
							generalPaneLayout
									.createParallelGroup(GroupLayout.Alignment.BASELINE)
									.addComponent(getUrlTextField(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE,
											GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
									.addComponent(getUrlLabel(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE,
											GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(
							generalPaneLayout
									.createParallelGroup(GroupLayout.Alignment.BASELINE)
									.addComponent(getDirectoryTextField(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE,
											GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
									.addComponent(getDirectoryButton(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE,
											GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
									.addComponent(getDirectoryLabel(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE,
											GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(
							generalPaneLayout
									.createParallelGroup(GroupLayout.Alignment.BASELINE)
									.addComponent(getRenameTextField(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 21,
											GroupLayout.PREFERRED_SIZE)
									.addComponent(getRenameLabel(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE,
											GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(
							generalPaneLayout
									.createParallelGroup(GroupLayout.Alignment.BASELINE)
									.addComponent(getThreadSpinner(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE,
											GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
									.addComponent(getThreadLabel(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE,
											GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)).addContainerGap());
		}
		return generalPane;
	}

	private JPanel getAdvancedPane() {
		if (advancedPane == null) {
			advancedPane = new JPanel();
			GroupLayout advancedPaneLayout = new GroupLayout(advancedPane);
			advancedPane.setLayout(advancedPaneLayout);
			advancedPaneLayout.setHorizontalGroup(advancedPaneLayout
					.createSequentialGroup()
					.addContainerGap()
					.addGroup(
							advancedPaneLayout
									.createParallelGroup()
									.addGroup(
											GroupLayout.Alignment.LEADING,
											advancedPaneLayout
													.createSequentialGroup()
													.addComponent(getMainUrlLabel(), GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE,
															GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
													.addComponent(getMainUrlTextField(), 0, 90, Short.MAX_VALUE))
									.addComponent(getScrollPane(), GroupLayout.Alignment.LEADING, 0, 150, Short.MAX_VALUE)).addContainerGap());
			advancedPaneLayout
					.setVerticalGroup(advancedPaneLayout
							.createSequentialGroup()
							.addContainerGap()
							.addGroup(
									advancedPaneLayout
											.createParallelGroup(GroupLayout.Alignment.BASELINE)
											.addComponent(getMainUrlTextField(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE,
													GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
											.addComponent(getMainUrlLabel(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE,
													GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(getScrollPane(), 0, 197, Short.MAX_VALUE)
							.addContainerGap());
		}
		return advancedPane;
	}

	private JLabel getUrlLabel() {
		if (urlLabel == null) {
			urlLabel = new JLabel();
			urlLabel.setText("URL:");
		}
		return urlLabel;
	}

	private JTextField getUrlTextField()
	{
		if( urlTextField == null )
		{
			urlTextField = new JTextField();
			urlTextField.getDocument().addDocumentListener( new DocumentAdapter(){
				@Override
				public void documentChanged( DocumentEvent e )
				{
					String url = urlTextField.getText();
					String name = "";
					if( true == ProtocolUtils.isSupportedURL( url ) )
						name = FileNameUtilities.getFileName( url, '/' );
					getRenameTextField().setText( name );
				}
			} );
			String clip = SystemClipboard.getClipboardContents();
			if( true == ProtocolUtils.isSupportedURL( clip ) )
			{
				urlTextField.setText( clip );
				urlTextField.selectAll();
			}
		}
		return urlTextField;
	}

	private JLabel getDirectoryLabel() {
		if (directoryLabel == null) {
			directoryLabel = new JLabel();
			directoryLabel.setText("Directory:");
		}
		return directoryLabel;
	}

	private JTextField getDirectoryTextField() {
		if (directoryTextField == null) {
			directoryTextField = new JTextField();
			directoryTextField.setText(Download.getApplication().getContext().getResourceMap().getString(Keys.DOWNLOAD_DIRECTORY));
		}
		return directoryTextField;
	}

	private JLabel getRenameLabel() {
		if (renameLabel == null) {
			renameLabel = new JLabel();
			renameLabel.setText("Rename:");
		}
		return renameLabel;
	}

	private JTextField getRenameTextField() {
		if (renameTextField == null) {
			renameTextField = new JTextField();
		}
		return renameTextField;
	}

	private SpinnerNumberModel getSpinnerModel() {
		if (null == spinnerModel)
			spinnerModel = new SpinnerNumberModel(5, 1, Short.MAX_VALUE, 1);
		return spinnerModel;
	}

	private JSpinner getThreadSpinner() {
		if (threadSpinner == null) {
			threadSpinner = new JSpinner(this.getSpinnerModel());
		}
		return threadSpinner;
	}

	private JLabel getThreadLabel() {
		if (threadLabel == null) {
			threadLabel = new JLabel();
			threadLabel.setText("Thread:");
		}
		return threadLabel;
	}

	private JLabel getMainUrlLabel() {
		if (mainUrlLabel == null) {
			mainUrlLabel = new JLabel();
			mainUrlLabel.setText("Main URL:");
		}
		return mainUrlLabel;
	}

	private JTextField getMainUrlTextField() {
		if (mainUrlTextField == null) {
			mainUrlTextField = new JTextField();
			mainUrlTextField.setEditable(false);
			mainUrlTextField.setDocument(this.getUrlTextField().getDocument());
		}
		return mainUrlTextField;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTextArea());
		}
		return scrollPane;
	}

	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
		}
		return textArea;
	}
}
